﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.ObjectModel;
using System.Data;
using System.Data.SqlClient;

namespace TinyCinema.Data
{
    public enum DeskStatus
    {
        Available,
        Ordered,
        Ordering
    }
    public class Desk:Execution
    {
        public string ID { get; set; }
        public string Range { get; set; }
        public DeskType Type { get; set; }
        public Room Room { get; set; }
        DeskStatus _Status;
        public DeskStatus Status
        {
            get { return _Status; }
            set
            {
                _Status = value;
                OnPropertyChanged("Status");
            }
        }
        int _TypeIndex;
        public int TypeIndex
        {
            get { return _TypeIndex; }
            set
            {
                if (value == -1) return;
                Type = DeskType.lstType[value];
                _TypeIndex = value;
                OnPropertyChanged("Type");
                if (IsInit) Save();
            }
        }
        public bool IsUsed {get;set;}
        int _Order;
        public int Order
        {
            get { return _Order; }
            set
            {
                IsUsed = (value == 0) ? false : true;
                OnPropertyChanged("IsUsed");
                if (value == _Order) return;
                
                _Order = value;
                OnPropertyChanged("Order");
                if (IsInit) Save();
            }
        }
        bool IsInit = true;
        public Desk(string _ID)
        {
            IsInit = false;
            ID = _ID;
            PutTableIntoDataSet(Tables.Desk);
            var tbl = ds.Tables[Tables.Desk];
            var Row = tbl.Select(DeskTable.ID + "='" + ID + "'")[0];
            Range = Row[DeskTable.Range] as string;
                 
            Room = new Room(Row[DeskTable.RoomID] as string,true);
            Type = new DeskType(Row[DeskTable.DeskTypeID] as string);
            foreach (var type in DeskType.lstType)
                if (Type.ID == type.ID)
                {
                    TypeIndex = DeskType.lstType.IndexOf(type);
                    break;
                }
            
            Order = (int)Row[DeskTable.Order];
            IsInit = true;
        }
        public Desk(Room _Room,string _Range,int _Order)
        {
            ID = IDGenerate(Tables.Desk);
            var lstDeskType = DeskType.lstType;
            if (lstDeskType.Count > 0) Type = lstDeskType[0];
            Room = _Room;
            Range = _Range;
            Order = _Order;
            Save();
        }
        public void Save()
        {
            PutTableIntoDataSet(Tables.Desk);
            var tbl = ds.Tables[Tables.Desk];
            var Rows = tbl.Select(DeskTable.ID + "='" + ID + "'");
            if (Rows.Length == 0)
            {
                var Row = tbl.NewRow();
                Row[DeskTable.ID] = ID;
                Row[DeskTable.DeskTypeID] = Type.ID;
                Row[DeskTable.Order] = Order;
                Row[DeskTable.Range] = Range;
                Row[DeskTable.RoomID] = Room.ID;
                tbl.Rows.Add(Row);
            }
            else
            {
                var Row = Rows[0];
                Row[DeskTable.DeskTypeID] = Type.ID;
                Row[DeskTable.Order] = Order;
                Row[DeskTable.Range] = Range;
                Row[DeskTable.RoomID] = Room.ID;
            }
            SaveCommand(Tables.Desk);
        }
        public void Delete()
        {
            var tbl = ds.Tables[Tables.Desk];
            var Row = tbl.Select(DeskTable.ID + "='" + ID + "'")[0];
            Row.Delete();
            SaveCommand(Tables.Desk);
        }
        
    }
}
